/**
 * Created by taohj on 2014/8/15.
 */
var model = new Object();


/**
 * 模块列表
 * */
model.initgrid = function () {


    model.datagrid = $("#admin_model_list_datagrid");

    model.datagrid.treegrid({
        url: '/admin/model/list',
        singleSelect: true,
        collapsible: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        sortName: "id",
        sortOrder: "desc",
        idField:'id',
        treeField:'name',
        animate:true,
        fit: true,
        pageList: [20, 50, 100, 200],
        pageSize: 20,
        toolbar: [
            {
                iconCls: 'icon-add',
                text: '新增',
                handler: function () {
                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title: '新增',
                        href: '/admin/model/createV',
                        modal: true,
                        width: 400,
                        top: 150
                    });
                }
            },
            '-',
            {
                iconCls: 'icon-edit',
                text: '更新',
                handler: function () {
                    var selectRow = model.datagrid.datagrid("getSelected");
                    if (!selectRow) {
                        //todo
                        return;
                    }

                    var id = selectRow.id;
                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title: '更新',
                        href: '/admin/model/modifyV' + '?id=' + id,
                        modal: true,
                        width: 400,
                        top: 150
                    });

                }
            },
            '-',
            {
                iconCls: 'icon-remove',
                text: '删除',
                handler: function () {

                    var selectRow = model.datagrid.datagrid("getSelected");
                    if (!selectRow) {
                        //todo
                        return;
                    }

                    $.messager.confirm('删除确认', '确定要删除此项么？ ', function (r) {
                        if (r) {
                            $.post('/admin/model/remove', {
                                id: selectRow.id
                            }, function (result) {
                                util.requestUtil(result, null, model.datagrid);
                            });
                        }
                    });
                }
            }
        ],
        columns: [
            [
                {field: "id", title: "编号", width: 60},
                {field: "name", title: "名称", width: 200},
                {field: "url", title: "地址", width: 200},
                {field: "type", title: "类型", width: 200,formatter:function(value){
                    return value?value.name:'';
                }},
                {field: "isLeaf", title: "是否为叶子", width: 200,formatter:function(value){
                    return value?value.name:'';
                }},
                {field: "sort", title: "排序", width: 200},
                {field: "description", title: "简介"}
            ]
        ]
    });
};


//新建提交
model.createSubmit = function (button) {
    var form = util.form(button);
    form.form('submit', {
        url: '/admin/model/create',
        success: function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result, dialog, model.datagrid)
        }
    });

};


//修改提交
model.modifySubmit = function (button) {
    var form = util.form(button);
    form.form('submit', {
        url: '/admin/model/modify',
        success: function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result, dialog, model.datagrid)
        }
    });
};


//搜索
model.searchSubmit = function (button) {
    var form = util.form(button);
    var params = util.serializeObject(form);
    model.datagrid.datagrid("load", params);
}

